package com.scrumtrek.trainings.tdd;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * User: neee
 * Date: 11.09.13
 * Time: 10:11
 * To change this template use File | Settings | File Templates.
 */
public class BookStore {
    private final IStore<Book> store;

    public BookStore(IStore<Book> store) {
        this.store = store;
    }

    public int getTotalBookAmount() {
        return store.getItems().size();
    }

    public void addBook(Book book) {
        store.addItem(book);
    }

    public Set<String> getAuthors() {
        Set<String> authors = new HashSet<String>();
        for (Book book : store.getItems()) {
            authors.add(book.getAuthor());

        }
        return authors;
    }

    public Set<String> getTitles() {
        Set<String> titles = new HashSet<String>();
        for (Book book : store.getItems()) {

            titles.add(book.getTitle() +add(book.getPublisher()) + add(book.getYear()));
        }
        return titles;
    }

    private String add(String in){
        if (in == null || in.isEmpty()){
            return "";
        }
        return " " + in;
    }

    public List<Book> findByAuthor(String author) {
        List<Book> result = new ArrayList<Book>();
        for (Book book : store.getItems()) {
            if(book.getAuthor().equals(author)){
                result.add(book);
            }
        }
        return result;
    }

    public List<Book> findByTitle(String title) {
        List<Book> result = new ArrayList<Book>();
        for (Book book : store.getItems()) {
            if(book.getTitle().equals(title)){
                result.add(book);
            }
        }
        return result;
    }

    public void sellBook(Book book) {
        store.sell(book);
    }
}
